package com.jrfid.smartcabinet.demo.model

import android.os.Parcel
import android.os.Parcelable
import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import com.blankj.utilcode.util.StringUtils
import com.jrfid.smartcabinet.demo.R
import java.io.Serializable

class CabinetConfigInfoModel() : Parcelable {

    var isMain: Boolean = false
    var name = ObservableField("")
    var readerIP = ObservableField("")
    var readerPort = ObservableField("")
    var icIP = ObservableField("")
    var icPort = ObservableField("")
    var fingerIP = ObservableField("")
    var fingerPort = ObservableField("")
    var address = ObservableField("")

    constructor(isMain: Boolean) : this() {
        this.isMain = isMain
    }

    constructor(parcel: Parcel) : this() {
        isMain = parcel.readInt() == 1
        name.set(parcel.readString() ?: "")
        readerIP.set(parcel.readString() ?: "")
        readerPort.set(parcel.readString() ?: "")
        icIP.set(parcel.readString() ?: "")
        icPort.set(parcel.readString() ?: "")
        fingerIP.set(parcel.readString() ?: "")
        fingerPort.set(parcel.readString() ?: "")
        address.set(parcel.readString() ?: "")
    }

    fun getPositionName(position: Int): String {
        return if (isMain) StringUtils.getString(R.string.text_cabinet_name_0) else StringUtils.getString(R.string.text_cabinet_name_1, position)
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeInt(if (isMain) 1 else 0)
        parcel.writeString(name.get())
        parcel.writeString(readerIP.get())
        parcel.writeString(readerPort.get())
        parcel.writeString(icIP.get())
        parcel.writeString(icPort.get())
        parcel.writeString(fingerIP.get())
        parcel.writeString(fingerPort.get())
        parcel.writeString(address.get())
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<CabinetConfigInfoModel> {
        override fun createFromParcel(parcel: Parcel): CabinetConfigInfoModel {
            return CabinetConfigInfoModel(parcel)
        }

        override fun newArray(size: Int): Array<CabinetConfigInfoModel?> {
            return arrayOfNulls(size)
        }
    }

}